package com.xmzs.midjourney.handler.listener;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.xmzs.common.core.utils.MapstructUtils;
import com.xmzs.midjourney.constant.MidjourneyConstant;
import com.xmzs.midjourney.domain.MjRoomMsg;
import com.xmzs.midjourney.domain.bo.MidjourneyDiscordMessageBO;
import com.xmzs.midjourney.domain.bo.MjRoomMsgBo;
import com.xmzs.midjourney.domain.vo.MjRoomMsgVo;
import com.xmzs.midjourney.enums.MidjourneyMsgStatusEnum;
import com.xmzs.midjourney.util.MjDiscordMessageUtil;
import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.entities.Message;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Objects;

/**
 * @author hncboy
 * @date 2023/5/18
 * Imagine 命令消息处理器
 * 消息接收 - Midjourney Bot: **[6601101751104431] blue sky --v 5 --s 250** - <@1013002753796219000> (Waiting to start)
 * 消息变更 - Midjourney Bot: **[6601101751104431] blue sky --v 5 --s 250** - <@1013002753796219000> (0%) (fast)
 * 消息变更 - Midjourney Bot: **[6601101751104431] blue sky --v 5 --s 250** - <@1013002753796219000> (15%) (fast)
 * 消息变更 - Midjourney Bot: **[6601101751104431] blue sky --v 5 --s 250** - <@1013002753796219000> (31%) (fast)
 * 消息变更 - Midjourney Bot: **[6601101751104431] blue sky --v 5 --s 250** - <@1013002753796219000> (46%) (fast)
 * 消息变更 - Midjourney Bot: **[6601101751104431] blue sky --v 5 --s 250** - <@1013002753796219000> (62%) (fast)
 * 消息变更 - Midjourney Bot: **[6601101751104431] blue sky --v 5 --s 250** - <@1013002753796219000> (78%) (fast)
 * 消息变更 - Midjourney Bot: **[6601101751104431] blue sky --v 5 --s 250** - <@1013002753796219000> (93%) (fast)
 * 消息接收 - Midjourney Bot: **[6601101751104431] blue sky --v 5 --s 250** - <@1013002753796219000> (fast)
 */
@Component
@Slf4j
public class ImagineDiscordMessageHandler extends AbstractDiscordMessageHandler {

    @Override
    public void onMessageReceived(Message message) {
        log.info("Imagine 收到消息");
        MidjourneyDiscordMessageBO messageBO = MjDiscordMessageUtil.matchImagineMessage(message);
        if (Objects.isNull(messageBO)) {
            return;
        }
        MjRoomMsg roomMidjourneyMsg = extractRoomMidjourneyMsgDO(message);
        if (Objects.isNull(roomMidjourneyMsg)) {
            return;
        }

        // 开始处理，不考虑消息乱序和丢失的情况
        if (MidjourneyConstant.WAITING_TO_START.equals(messageBO.getStatus())) {
            // 队列排队再回调好像没有这个，还需确认一下
            roomMidjourneyMsg.setDiscordStartTime(new Date());
            roomMidjourneyMsg.setStatus(MidjourneyMsgStatusEnum.MJ_IN_PROGRESS);
            roomMidjourneyMsg.setDiscordMessageId(message.getId());
        }
        // 处理成功
        else {
            finishImageTask(roomMidjourneyMsg, message);
        }

        roomMidjourneyMsg.setResponseContent(message.getContentRaw());
        roomMidjourneyMsgService.update(roomMidjourneyMsg,new LambdaUpdateWrapper<>(MjRoomMsg.class).eq(MjRoomMsg::getId,roomMidjourneyMsg.getId()));
//        MjRoomMsgBo updateMjRoomMsg = MapstructUtils.convert(roomMidjourneyMsg, MjRoomMsgBo.class);
//        roomMidjourneyMsgService.updateByBo(updateMjRoomMsg);
    }

    @Override
    public void onMessageUpdate(Message message) {
        MjRoomMsg roomMidjourneyMsg = extractRoomMidjourneyMsgDO(message);
        if (Objects.isNull(roomMidjourneyMsg)) {
            return;
        }

        roomMidjourneyMsg.setDiscordMessageId(message.getId());
        roomMidjourneyMsg.setResponseContent(message.getContentRaw());
        roomMidjourneyMsg.setDiscordImageUrl(message.getAttachments().get(0).getUrl());
        // 下载原图
        roomMidjourneyMsg.setOriginalImageName(downloadOriginalImage(roomMidjourneyMsg.getDiscordImageUrl(), roomMidjourneyMsg.getId()));
        // 过程图片就不压缩了
        roomMidjourneyMsg.setCompressedImageName(roomMidjourneyMsg.getOriginalImageName());
        MjRoomMsgBo updateMjRoomMsg = MapstructUtils.convert(roomMidjourneyMsg, MjRoomMsgBo.class);
        roomMidjourneyMsgService.updateByBo(updateMjRoomMsg);
    }

    private MjRoomMsg extractRoomMidjourneyMsgDO(Message message) {
        MidjourneyDiscordMessageBO messageBO = MjDiscordMessageUtil.matchImagineMessage(message);
        if (Objects.isNull(messageBO)) {
            return null;
        }

        // 提取房间消息 id
        Long roomMjMsgId = MjDiscordMessageUtil.findMsgIdByFinalPrompt(messageBO.getPrompt());
        if (Objects.isNull(roomMjMsgId)) {
            return null;
        }
        MjRoomMsg roomMidjourneyMsg = roomMidjourneyMsgService.getOne(new LambdaQueryWrapper<MjRoomMsg>().eq(MjRoomMsg::getId,roomMjMsgId));//roomMidjourneyMsgService.queryById(roomMjMsgId);
        if (Objects.isNull(roomMidjourneyMsg)) {
            return null;
        }
//        MjRoomMsg roomMidjourneyMsg = MapstructUtils.convert(roomMidjourneyMsgVo, MjRoomMsg.class);
        return roomMidjourneyMsg;
    }
}
